<?php

namespace App\Http\Controllers\Personal;

use App\Model\Leave;
use App\Model\Role;
use App\Model\Sch_desc;
use App\Model\Staff;
use App\Model\Staff_task;
use App\Model\Stop_settle;
use App\Model\Task;
use App\Model\Task_user;
use App\Model\Workflow;
use App\Model\Workflow_desc;
use App\Model\Workflow_group;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;
//任务执行&&接取任务
class TaskController extends CommonController
{
    //
	protected $task;
	protected $leave;
	protected $staff;
	protected $Sch_desc;
	protected $Stop_settle;
	protected $Workflow;
	protected $Workflow_desc;
	protected static $company_id;
	protected static $uid;
	protected $Role;
	protected $staff_stak;
	protected $Task_user;
	protected static $userInfo;
	protected $Workflow_group;
	public function __construct()
	{
		parent::__construct();

		$this->task=new Task();
		$this->staff_stak=new Staff_task();
		$this->leave=new Leave();
		$this->staff=new Staff();
		$this->Role=new Role();
		$this->Sch_desc=new Sch_desc();
		$this->Stop_settle=new Stop_settle();
		$this->Workflow=new Workflow();
		$this->Workflow_desc=new Workflow_desc();
		self::$uid=Auth::user()->id;
		self::$company_id=Auth::user()->company_id;
		self::$userInfo=Auth::user();
		$this->Task_user=new Task_user();
		$this->Workflow_group=new Workflow_group();

	}


//	获取任务列表
	public function task_list(Request $request){
		$store_id=$this->store_id;
		$result=$this->task->lists($store_id,self::$uid);
		$data=[];
		foreach($result as $k=>$v){
			if(!count($v['task_user'])){
				$data[$k]['task_id']=$v['id'];
				$data[$k]['name']=$v['store_station']['name'];
				$data[$k]['store_station_id']=$v['store_station_id'];
				$data[$k]['num']=$v['num'];
				$data[$k]['status']=$v['store_station']['status']==1?'长期有效':'短期有效';
				$data[$k]['start_time']=$v['store_station']['start_time'];
				$data[$k]['end_time']=$v['store_station']['end_time'];
			}
		}
		$array=[];
		foreach ($data as $kk=>$vv){
			$array[]=$vv;
		}

		return $this->success($array);
	}



//	获取任务详情
	public function receipt_desc($id){
		$data=$this->task->desc($id);
		$result['station_name']=$data->store_station->name;
		$result['store_station_id']=$data->store_station_id;
		$result['status']=$data->store_station->station->status==1?'永久有效':'短期有效';
		$result['start_time']=$data->store_station->start_time;
		$result['end_time']=$data->store_station->end_time;
		$result['num']=$data->store_station->num;
		$result['comment']=$data->store_station->station->comment;
		return $this->success($result);
	}

//	立即接单
	public function receipt(Request $request){
		$data['task_id']=$request->task_id;
		$data['uid']=Auth::user()->id;
		$data['store_id']=$this->store_id;
		$data['store_station_id']=$request->store_station_id;
		$data['receive_time']=Carbon::now();
		DB::beginTransaction();
		try{
//			$taff_id=$this->staff->addStaff_($data['uid'],$data['store_id'],$data['store_station_id']);
			$taff_id=$this->staff->addStaff($data['uid'],$data['store_id'],$data['store_station_id']);
			$data['staff_id']=$taff_id;
			DB::table('task_user')->insert(['task_id'=>$data['task_id'],'uid'=>$data['uid']]);
			$result=$this->staff_stak->create($data);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}

	}



//	任务执行首页
















//	终止结算
	public function termination(Request $request){
		$termination=isset($request->termination)?$request->termination:date("Y-m-d H:i:s",time());
		$id=$request->id;
		$staff_task=Staff_task::where(['id'=>$id])->first();
//		添加终止结算
		$data['uid']=self::$uid;
		$data['staff_id']=$staff_task['staff_id'];
		$data['staff_task_id']=$id;
		$data['termination']=$termination;
		$data['store_id']=$staff_task['store_id'];
		$this->validate($request,[
			'id'=>'required'
		]);
		DB::beginTransaction();
		try{
			$result=$this->staff_stak->where(['id'=>$id])->update(['termination'=>$termination,'status'=>5]);
			$res=$this->Stop_settle->create($data);
			$id=$res->id;
			$this->binding($id);
			DB::commit();

		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->updated($res);
		}else{
			return $this->failed($message);
		}
	}

//	终止结算详情
	public function termination_desc(Request $request){
		$id=$request->id;
		try{
			$data=$this->staff_stak->termination_desc($id);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			$leave_time=$this->leave_time($id);
			$sign_day=$this->sign_day($data['staff_id'],$data['task']['start_day'],$data['task']['end_day']);
			$data['sign_day']=$sign_day;
			$data['leave_time']=$leave_time;
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}


//	中止时间
	public function leave_time($id){
		return $this->leave->where(['id'=>$id])->sum('time');
	}

//	签到天数
	public function sign_day($staff_id,$start_day,$end_day){
		$data=$this->Sch_desc
			->whereBetween('time',[$start_day,$end_day])
			->where(function ($query) use($staff_id){
				$query->where(['staff_id'=>$staff_id]);
				$query->whereNotNull('sign_start');
				$query->whereNotNull('sign_end');
			})->get();
		return count($data);
	}

//  绑定审核流程
	public function binding($id){
		$workflowData=$this->Workflow->where(['company_id'=>self::$company_id,'style'=>4,'status'=>1])
			->first();
		$group_id=$this->belongsToGroup(self::$userInfo->department);
//		查询该用户的审批流
		$workflow_id=$this->Workflow_group->getworkflow_id($group_id,4);
		$workflow_desc['lovel_uid']=$this->Role->Role_user($workflowData->one);
		$workflow_desc['code']=Uuid::uuid4();
		foreach($workflow_desc['lovel_uid'] as $kk=>$vv){
			$data[$kk]['workflow_id']=$workflow_id['id'];
			$data[$kk]['from_uid']=self::$uid;
			$data[$kk]['lovel_role']=$workflowData->one;
			$data[$kk]['lovel_uid']=$vv;
			$data[$kk]['stop_id']=$id;
			$data[$kk]['code']=$workflow_desc['code'];
			$data[$kk]['type']=4;
			$data[$kk]['created_at']=date("Y-m-d H:i:s",time());
		}
		DB::table('workflow_desc')->insert($data);
	}



}
